// ClearApp.cpp
// ClearApp
// Clear Application Database / Clears an application database by removing all records
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// Please send correspondence to:
//
// theproteaproject@gmail.com
//

#include "../SDK/ToolsInterface.h"

//
// Global variables
//
int app_number;

class XCClearApp : public XCAppInterface {
public:
	void ClearData() {
		XCTxn* txn = NULL;
		XCSQLGen query( this );
		const buffer_header* header = GetAPPDICHeader();

		try {
			txn = GetDatabase()->CreateTxn();
			cout << "Clearing application data.." << endl;
			buffer_format* format = (buffer_format*)FixOffset( header->formats.offset );
			for (int i=0; i<header->formats.count; i++) {
				if (format->type < 3) {
					XCBuffer* buffer = CreateBuffer( format->number );
					query.MakeDELETE( buffer );
					txn->ExecuteSQL( &query, SQL_NO_RESULT );
				};
				format++;
			};

			txn->EndTxn( true );
		} catch( ... ) {
			if (txn) {
				txn->EndTxn( false );
				txn->Release();
			};
		};
	};
};


///////////////////////////////////////////////////
//
// Program entry point
//
///////////////////////////////////////////////////

int main(int argc, char* argv[]) {
	XCAppOptions myopts;
	int result = 0;

	Protea::ShowIntro( "ClearApp" );

	if (argc > 1 && !strcmp(argv[1],"-L"))
	{
		Protea::ShowLicense();
		return 0;
	};

	XCClearApp app;

	try {
		if (argc != 2) {
			cout << "Usage: ClearApp ApplicationNumber" << endl;
			return 1;
		};

		app_number = atoi( argv[1] );
		if (app_number < 3) {
			cout << "Can't let you do that!" << endl;
			return 255;
		};

		Protea::Initialize();

		app.GetOptions( myopts );
		myopts.debug_level_cutoff[ TCL_APPINTERFACE ] = TLV_MEDIUM;
		for ( int i = 0; i < num_tc; i++ )
			myopts.debug_level_cutoff[ i ] = 0xFF;
//		myopts.debug_type_filter = 0xFFFF;
		myopts.debug_type_filter = 0;
		myopts.no_transactions = true;
		app.SetOptions( myopts );

		// Initialize
		app.Initialize( "ClearApp", app_number );

		//
		// Clear the app data
		//
		app.ClearData();
		cout << endl << "Application data cleared..." << endl << endl;
	} catch( XCToolsError& error ) {
		app.ToolsTrace( TRC_APPINTERFACE, TLV_LOW, "Caught tools error" );
		XCOutputCallStack( &app, error.GetErrorNumber(),error.GetDescription() );
		result = error.GetErrorNumber();
	} catch( ... ) {
		result = 1;
		app.ToolsTrace( TRC_APPINTERFACE, TLV_LOW, "Caught unknown error" );
		XCOutputCallStack( &app, -1, NULL );
	};

	app.Uninit();

	Protea::Uninitialize();

	return result;
};
